package com.stockz.ui.swing.charting.painter.chart;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.List;

import com.stockz.core.model.Bar;
import com.stockz.ui.swing.charting.util.MathUtil;
import com.stockz.ui.swing.charting.view.StockChartPanel;
import com.stockz.ui.swing.charting.view.StockChartPanelSkin;
import com.stockz.ui.swing.charting.view.StockChartPanelState;

public class RectangleVolumePainter {

	public static void paint(Graphics2D g, StockChartPanel stockChartPanel, Rectangle rectangle) {
		StockChartPanelSkin stockChartPanelSkin = stockChartPanel.getStockChartPanelSkin();
		StockChartPanelState stockChartPanelState = stockChartPanel.getStockChartPanelState();
		g.setColor(stockChartPanelSkin.getVolumeColor());

		List<Bar> barList = stockChartPanelState.getBarList();
		for(int i = stockChartPanelState.getEndIndex()-1; i >= stockChartPanelState.getStartIndex(); i--){
			Bar bar = barList.get(i);
			int x = MathUtil.fitX(rectangle.x, stockChartPanelState.getTimeUnitSpace(), stockChartPanelState.getEndIndex(), i);
			
			int volumeY = MathUtil.fitY(rectangle.y, 
					rectangle.y+rectangle.height, 
					stockChartPanelState.getMinimumVolumeVisible(), 
					stockChartPanelState.getMaximumVolumeVisible(), 
					bar.getVolume(),
					stockChartPanelState.isSemiLog());
			
			g.fillRect(
					x - stockChartPanelState.getTimeUnitSpace()/2,
					volumeY,
					stockChartPanelState.getTimeUnitSpace(),
					rectangle.y+rectangle.height-volumeY);
		}
	}
}
